<?php

class Sistema_Form_Material extends Zend_Form {

    public function init() {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        //$url = $front->getBaseUrl().'/'.$module.'/'.$controller;

        $this->setName('form_' . $controller);
        $this->setAttrib('class', 'formCtrl');

        $id = $front->getRequest()->getParam('id', 0);
        $for_id = $front->getRequest()->getParam('for_id', 0);

        $url = $front->getBaseUrl() . '/' . $module . '/' . $controller . '/index/for_id/' . $for_id;


        $el = new Zend_Form_Element_Hidden('MAT_ID', array('disableLoadDefaultDecorators' => true));
        $el->addDecorators(array(array('ViewHelper'), array('Errors')))
                ->addFilter('Int');
        $this->addElement($el);

        //$el = new Zend_Form_Element_Hidden('FOR_ID', array('disableLoadDefaultDecorators' => true));
        //$el->addDecorators(array(array('ViewHelper'), array('Errors')))
        //        ->addFilter('Int');
        //$el->setValue($for_id);
        //$this->addElement($el);

        $el = new Zend_Form_Element_Text('MAT_NOM');
        $el->setLabel('Nome')
                ->setRequired(true)
                ->addFilters(array('StripTags', 'StringTrim'))
                ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);

        $el = new Zend_Form_Element_Text('MAT_COM1');
        $el->setLabel('Composição 1')
                ->addFilters(array('StripTags', 'StringTrim'))
                ->addValidator('NotEmpty');
        $this->addElement($el);

        $el = new Zend_Form_Element_Text('MAT_COM2');
        $el->setLabel('Composição 2')
                ->addFilters(array('StripTags', 'StringTrim'))
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('MAT_COM3');
        $el->setLabel('Composição 3')
                ->addFilters(array('StripTags', 'StringTrim'))
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('MAT_PES');
        $el->setLabel('Gramatura')
                ->addFilters(array('StripTags', 'StringTrim'))
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $html = '';
        $htmli = '';
        $obj = new Sistema_Model_DbTable_Catsegmento();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        $arr_set = array();
        $count_msc = 0;

        if ($id > 0) {
            $msc = new Sistema_Model_DbTable_MaterialHasCat();
            $msc = $msc->fetchRow('MAT_ID = ' . $id);

            if (isset($msc['CAT_ID']) && $msc['CAT_ID'] > 0) {
                $count_msc = 1;
            }
        }

        foreach ($row as $r) {

            $count = 0;
            $checked = '';

            $fsc = new Sistema_Model_DbTable_FornecedorHasSetcat();
            $fsc = $fsc->fetchAll('FOR_ID = ' . $for_id . ' and CTS_ID = ' . $r['CTS_ID'])->toArray();
            foreach ($fsc as $f) {
                $arr_set[$f['CAT_ID']] = $f['CAT_ID'];
                $count = 1;
            }

            $htmli = '';
            if ($count == 1) {

                $checked = 'checked=""';

                $htmli = '<select id="SEG_CAT[' . $r['CTS_ID'] . '][]" name="SEG_CAT[' . $r['CTS_ID'] . '][]" class="chosen-select" multiple="multiple" style="width:500px;">';

                //$cat = new Sistema_Model_DbTable_Categoria;
                $cat = new Sistema_Model_DbTable_FornecedorHasSetcat;
                $cat = $cat->fetchAll('FOR_ID = ' . $for_id . ' and CTS_ID = ' . $r['CTS_ID']);

                foreach ($cat as $c) { //$arr[$r['MTL_ID']] = $r['MAT_NOM']; 
                    //$selected = array_search($c['CAT_ID'], $arr_set) > '' ? 'selected=""':'';
                    $catn = new Sistema_Model_DbTable_Categoria();
                    $catn = $catn->fetchRow('CAT_ID = ' . $c['CAT_ID']);

                    $selected = 'selected=""';

                    // se tiver registro na MaterialHasCat
                    if ($count_msc > 0) {
                        
                        // seleciona o registro
                        $msc = new Sistema_Model_DbTable_MaterialHasCat();
                        $msc = $msc->fetchRow('CAT_ID = ' . $c['CAT_ID'].' and MAT_ID = ' . $id);

                        $selected = '';
                        if (isset($msc['CAT_ID']) && $msc['CAT_ID'] > 0 && $c['CAT_ID'] == $msc['CAT_ID']) {
                            $selected = 'selected=""';
                        }
                        $htmli .= '<option ' . $selected . ' value="' . $c['CAT_ID'] . '">' . $catn['CAT_NOM'] . '</option>';
                    } else {
                        $htmli .= '<option ' . $selected . ' value="' . $c['CAT_ID'] . '">' . $catn['CAT_NOM'] . '</option>';
                    }
                }
                $htmli .= '</select><br><br>';


                $html .= '<div><label>' . $r['CTS_NOM'] . '</label>';
                $html .= '<div id="cat' . $r['CTS_ID'] . '">' . $htmli . '</div></div>';
            }
        }

        $el = new Zend_Form_Element('SEG_IDS', array('helper' => 'formNote', 'value' => $html));
        $el->setLabel('Segmento');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Checkbox('MAT_STA', array('checked' => 'checked'));
        $el->setLabel('Status');
        $this->addElement($el);

        $btn = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true));
        $btn->setLabel('Gravar');
        $btn->addDecorators(array(array('ViewHelper'), array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);

        $btn = new Zend_Form_Element_Button('Cancelar', array('disableLoadDefaultDecorators' => true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="' . $url . '" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
    }

}
